<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2016-6-21
 * Func   菜单api
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$menuid = intval($_REQUEST['menuid']);

$str = '';

if($a == 'top') //  顶部菜单
{
    //  用户组
    $gid = '';
    if($_Y['user']['gid'] > 1)
    {
        !$_Y['group']['permit'] && exit();
        
        $gid = ' AND id IN ('.$_Y['group']['permit'].') ';
    }
    
    $menu = $db->getall('SELECT * FROM '.Table('admin_menu').' WHERE pid = 0 AND hide = 0 '.$gid.' ORDER BY sort DESC');
    
    foreach($menu as $key => $val)
    {
        $url = '?m='.$val['m'].'&c='.$val['c'].'&a='.$val['a'];
        $str.= '<li id="_M'.$val['id'].'" class="top_menu'.(!$key ? ' on' : '').'"><a href="javascript:void(0);" onclick="_M('.$val['id'].',\''.$url.'\')">'.$val['name'].'</a></li>';
    }
}
else if($a == 'left')    //  左侧
{
    !$menuid && exit();
    
    $gid = '';
    if($_Y['user']['gid'] > 1)
    {
        !$_Y['group']['permit'] && exit();
        
        $gid = ' AND id IN ('.$_Y['group']['permit'].') ';
    }
	
    $menu = $db->getall('SELECT * FROM '.Table('admin_menu').' WHERE pid = '.$menuid.' AND hide = 0 '.$gid.' ORDER BY sort DESC');
    foreach($menu as $val)
    {
        if($val['son'])
        {
            $str .= '<h3 class="f14" onclick="_MS(this)"><i class="fa fa-fw fa-indent"></i>'.$val['name'].'<span class="fa arrow" title="展开与收缩"></span></h3>';
            $str .=  '<ul>';
            
            $submenu = $db->getall('SELECT * FROM '.Table('admin_menu').' WHERE pid = '.$val['id'].' AND hide = 0 '.$gid.' ORDER BY sort DESC');
            foreach($submenu as $val2)
            {
                $url = '?m='.$val2['m'].'&c='.$val2['c'].'&a='.$val2['a'];
                $str .= '<li id="_MP'.$val2['id'].'" class="sub_menu"><a href="javascript:void(0);" onclick="_MP('.$val2['id'].',\''.$url.'\');">'.$val2['name'].'</a></li>';
            }
            
            $str .= '</ul>';
        }
        else
        {
            $url = '?m='.$val['m'].'&c='.$val['c'].'&a='.$val['a'];
            $str .= '<li id="_MP'.$val2['id'].'" class="sub_menu"><a href="javascript:void(0);" onclick="_MP('.$val2['id'].',\''.$url.'\');">'.$val['name'].'</a></li>';
        }
    }
}
else if($a == 'pos')    //  当前位置
{
    !$menuid && exit();
    
    $pos = [];
    $tmp = $db->getrow('SELECT name,pid FROM '.Table('admin_menu').' WHERE id = '.$menuid);
    $pos[] = $tmp['name'];
    while($tmp['pid'])
    {
        $tmp = $db->getrow('SELECT name,pid FROM '.Table('admin_menu').' WHERE id = '.$tmp['pid']);
        $pos[] = $tmp['name'];
    }
    
    $str = implode(' > ',array_reverse($pos));
}

echo $str;

?>